﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Collections.ObjectModel;
using System.ServiceModel.Activation;

namespace AteneoLibriServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AteneoLibriService" in both code and config file together.   
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class AteneoLibriService : IAteneoLibriService
    {

        /// <summary>
        /// Connessione al database
        /// </summary>
        AteneoLibriEntityFramework.AteneoLibriEntities db = new AteneoLibriEntityFramework.AteneoLibriEntities();

        #region University
        public List<AteneoLibriEntityFramework.University> getUniversities()
        {
            IEnumerable<AteneoLibriEntityFramework.University> universities = new List<AteneoLibriEntityFramework.University>();
            universities = (from u in db.Universities select u);
            return universities.ToList();
        }

        public AteneoLibriEntityFramework.University getUniversity(Guid idUniversity)
        {
            return null;
        }
        #endregion


        #region Users
        public List<AteneoLibriEntityFramework.User> getUsers() 
        {
            IEnumerable<AteneoLibriEntityFramework.User> usersList = new List<AteneoLibriEntityFramework.User>();
            usersList = (from u in db.Users select u);
            return usersList.ToList();
        }

        public AteneoLibriEntityFramework.User getUserByEmailAndPsw(String email, String password)
        {
            return null;
        }

        public List<AteneoLibriEntityFramework.User> getUsersByUniversityId(Guid universityId)
        {
            return null;
        }

        public bool insertUser(AteneoLibriEntityFramework.User user)
        {
            bool notYetRegistered = false;
            bool result = false;

            try
            {
                notYetRegistered = CheckExistingRegistration(user);
            }
            catch (Exception)
            {
                notYetRegistered = false;
            }
            

            if (notYetRegistered)
            {
                db.AddToUsers(user);
                db.SaveChanges();
                result = true;
            }

            return result;
        }

        private bool CheckExistingRegistration(AteneoLibriEntityFramework.User user)
        {
            throw new NotImplementedException();
        }

        #endregion      

    }
}
